package com.wuseguang.springcloud.controller;


import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.entity.Teacher;
import com.wuseguang.springcloud.service.TeacherService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 教师表/教职工表 前端控制器
 * </p>
 *
 * @author
 * @since 2019-12-23
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TeacherService teacherServiceImpl;


    /**
     * 查询教师列表
     *
     * @param request
     * @param name    姓名
     * @param id      查询id
     * @param type    查询类型 1部门，2年级，3班级
     * @param indexNo 页码
     * @return
     */
    @RequestMapping("/findTeacherAll")
    public Result findTeacherAll(RequestParameterWrapper request,
                                 @RequestParam(value = "name", required = false) String name,
                                 @RequestParam(value = "phone", required = false) String phone,
                                 @RequestParam(value = "id", required = false) Integer id,
                                 @RequestParam(value = "type", required = false) Integer type,
                                 @RequestParam(value = "roleId", required = false) Integer roleId,
                                 @RequestParam(value = "indexNo", required = false, defaultValue = "1") Integer indexNo,
                                 @RequestParam(value = "rows", required = false, defaultValue = "20") Integer rows) {

        logger.info("查询教师信息 TeacherController -> findTeacherAll");
        //获取学校id
        String schoolId = request.getParam("schoolId");

        return teacherServiceImpl.findTeacherAll(schoolId, name, id, type, roleId, indexNo, rows, phone);
    }

    /**
     * 删除教师信息
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/deleteTeacherByid")
    public Result deleteTeacherByid(RequestParameterWrapper request,
                                    @RequestParam(value = "id", required = false) String id) {
        logger.info("删除教师的信息 TeacherController -> deleteTeacherByid");
        /*//获取管理员状态
        String admin = request.getParam("admin");
        //判断是否是管理员
        if(!"0".equals(admin)){
            logger.info("非管理员不可以删除教师信息");
            return new Result(CodeUtil.NON_ADMINISTRATOR_UNAVAILABLE);
        }*/
        return teacherServiceImpl.deleteTeacherByid(id);
    }

    /**
     * 重置教师密码
     *
     * @param request
     * @param id      教师id
     * @return
     */
    @RequestMapping("/updateTeacherPassword")
    public Result updateTeacherPassword(RequestParameterWrapper request,
                                        @RequestParam(value = "id", required = false) Integer id,
                                        @RequestParam(value = "onePassWord", required = false) String onePassWord,
                                        @RequestParam(value = "twoPassWord", required = false) String twoPassWord) {
        logger.info("重置教师密码 TeacherController -> updateTeacherPassword ");
        /*//获取管理员状态
        String admin = request.getParam("admin");
        //判断是否是管理员
        if(!"0".equals(admin)){
            logger.info("非管理员不可以重置教师密码");
            return new Result(CodeUtil.NON_ADMINISTRATOR_UNAVAILABLE);
        }*/
        //判断密码是否为空
        if (StringUtils.isEmpty(onePassWord) || StringUtils.isEmpty(twoPassWord)) {
            logger.info("修改的密码为空，不执行修改。");
            return new Result(CodeUtil.THE_PASSWORD_IS_EMPTY);
        }
        //判断是否相同
        if (!onePassWord.equals(twoPassWord)) {
            return new Result(CodeUtil.PASSWORDS_ARE_INCONSISTENT);
        }

        return teacherServiceImpl.updateTeacherPassword(id, onePassWord);
    }


    /**
     * 重置全校的教师密码
     *
     * @param request
     * @return
     */
    @RequestMapping("/updateAllTeacherPassword")
    public Result updateAllTeacherPassword(RequestParameterWrapper request,
                                           @RequestParam(value = "onePassWord", required = false) String onePassWord,
                                           @RequestParam(value = "twoPassWord", required = false) String twoPassWord) {

        logger.info("重置这个学校的所有教师密码 TeacherController -> updateAllTeacherPassword ");

        //获取管理员状态
        /*String admin = request.getParam("admin");
        //判断是否是管理员
        if(!"0".equals(admin)){
            logger.info("非管理员不可以重置教师密码");
            return new Result(CodeUtil.NON_ADMINISTRATOR_UNAVAILABLE);
        }*/
        //判断密码是否为空
        if (StringUtils.isEmpty(onePassWord) || StringUtils.isEmpty(twoPassWord)) {
            logger.info("修改的密码为空，不执行修改。");
            return new Result(CodeUtil.THE_PASSWORD_IS_EMPTY);
        }
        //判断是否相同
        if (!onePassWord.equals(twoPassWord)) {
            return new Result(CodeUtil.PASSWORDS_ARE_INCONSISTENT);
        }
        //学校id
        String schoolId = request.getParam("schoolId");

        return teacherServiceImpl.updateAllTeacherPassword(schoolId, onePassWord);
    }

    /**
     * 检测教师信息是否正确
     *
     * @param request
     * @param teacher 教师信息
     * @return
     */
    @RequestMapping("/teacherDetection")
    public Result teacherDetection(RequestParameterWrapper request,
                                   @RequestParam(value = "teacher", required = false) String teacher) {
        logger.info("检测教师信息 TeacherController -> teacherDetection");
        //获取学校id
        String schoolId = request.getParam("schoolId");
        //判断是否为空
        if (StringUtils.isEmpty(teacher)) {
            return new Result(CodeUtil.NO_TEACHER_DATA);
        }

        return teacherServiceImpl.teacherDetection(schoolId, teacher);
    }

    /**
     * 批量添加教师信息
     *
     * @param request
     * @param teacher 教师信息
     * @return
     */
    @RequestMapping("/batchAdditionTeacher")
    public Result batchAdditionTeacher(RequestParameterWrapper request,
                                       @RequestParam(value = "teacher", required = false) String teacher) {

        logger.info("批量添加教师信息 TeacherController -> batchAdditionTeacher");
        //获取学校id
        String schoolId = request.getParam("schoolId");
        //判断是否为空
        if (StringUtils.isEmpty(teacher)) {
            return new Result(CodeUtil.NO_TEACHER_DATA);
        }
      /*  //获取管理员状态
        String admin = request.getParam("admin");
        //判断是否是管理员
        if(!"0".equals(admin)){
            logger.info("非管理员不可以操作");
            return new Result(CodeUtil.NON_ADMINISTRATOR_UNAVAILABLE);
        }*/
        return teacherServiceImpl.batchAdditionTeacher(schoolId, teacher);
    }

    /**
     * 单个添加教师信息
     *
     * @param request
     * @param teacher 教师信息
     * @return
     */
    @RequestMapping("/addTeacher")
    public Result addTeacher(RequestParameterWrapper request, @RequestBody Teacher teacher) {
        logger.info("添加教师信息 TeacherController -> addTeacher");
        //获取学校id
        String schoolId = request.getParam("schoolId");

        return teacherServiceImpl.addTeacher(schoolId, teacher);
    }


    /**
     * 修改教师信息
     *
     * @param request
     * @param teacher
     * @return
     */
    @RequestMapping("/updateTeacher")
    public Result updateTeacher(RequestParameterWrapper request, @RequestBody Teacher teacher) {
        logger.info("修改教师信息 StudentController -> updateTeacher");
        //学校id
        String schoolId = request.getParam("schoolId");
        teacher.setSchoolId(Integer.valueOf(schoolId));

        return teacherServiceImpl.updateTeacher(teacher);
    }

    /**
     * 导出教师模板
     *
     * @param request
     * @param response
     */
    @RequestMapping("/exportTeacherTemplate")
    public void exportTeacherTemplate(RequestParameterWrapper request,
                                      HttpServletResponse response) {
        logger.info("导出学生模板");
        teacherServiceImpl.exportTeacherTemplate(request, response);
    }

    /**
     * 导出教师信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/exportTeacher")
    public void exportTeacher(RequestParameterWrapper request, HttpServletResponse response) {
        logger.info("导出教师信息");
        teacherServiceImpl.exportTeacher(request, response);
    }

    /**
     * 查询学校教师信息
     * @param request
     * @return
     */
    @RequestMapping("/findShcoolTeacher")
    public Result findShcoolTeacher(RequestParameterWrapper request){
        logger.info("查询学校教师信息");
        //学校id
        String schoolId = request.getParam("schoolId");
        return teacherServiceImpl.findShcoolTeacher(schoolId);
    }

    
}